import axios from "axios";
import type { Method } from "axios";
import type { UserInfo } from './AccountStore';
import AccountStore from "./AccountStore";
// 定义接口路径
enum RequestPath {
  login = "/users/login",
  signup = "/users/signup",
  operationalAdd="/operational/add",
  operationalGet="/operational/get",
  operationalUpdate="/operational/update",
  goodsCategoryGet="/goods/category/get",
  goodsCategoryAdd="/goods/category/add",
  goodsCategoryUpdate="/goods/category/update",
  goodsGet="/goods/get",
  goodsAdd="/goods/add",
  goodsUpdate="/goods/update",
  goodsDelete="/goods/delete",
  goodsListGet="/goods/list/get",
  orderListGet = '/order/list/get',
  orderUpdate = '/order/update',
  evaluationUpdate = '/evaluation/update',
  evaluationListGet = '/evaluation/list/get',
  statisticalGet = '/statistical/get'
}
// 创建一个请求实例
const worker = axios.create({
  baseURL: 'http://localhost:3000/api/v1',
  timeout: 5000,
  headers: {}
});
// 添加请求拦截器 - 自动添加token
worker.interceptors.request.use((config) => {
  // 从AccountStore获取token
  const token = AccountStore().token;
  if (token) {
    config.headers.token = token;
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});
// 响应拦截器
worker.interceptors.response.use((response) => {
  return response.data;
}, (error) => {
  if (error.response && error.response.status === 401) {
    AccountStore().logout();
    window.location.href = '/#/login';
  }
  return Promise.reject(error);
});
// 定义登录注册的接口数据返回结构
export interface LoginResponseData {
  msg: 'error' | 'ok';
  error?: string;
  info?: UserInfo;
  token?: string;
}
export interface OperationalItemData {
  id: number;
  name: string;
  status: number;
  created_at: string;
  cover: string;
  content: string;
  uri: string;
}
export interface OperationalResponseData {
  msg: 'error' | 'ok';
  error?: string;
  datas?: OperationalItemData[];
}
export interface CategoryItemData {
  id: number,
  name: string,
  description: string,
  created_at: string,
  sort: number
}
export interface CategoryResponseData {
  msg: 'error' | 'ok',
  error?: string,
  datas?: CategoryItemData[]
}
export interface GoodsItemData {
  id: number,
  name: string,
  description: string,
  created_at: string,
  detail?: string,
  price: number,
  discounted_price: number,
  stock: number,
  image: string,
  status: number,
  brand: string,
  category: CategoryItemData
}
export interface ShopCarGoodsData {
    count: number,
    item: GoodsItemData
}
export interface OrderData {
    id: number,
    user_id: number,
    goods?: ShopCarGoodsData[],
    address: string,
    price: number,
    save_price:number,
    state: 0 | 1 | 2 | 3,
    created_at: string
}
// 订单接口返回的数据结构模型
export interface OrderResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: OrderData[]
}
export interface GoodsResponseData {
  msg: 'error' | 'ok',
  error?: string,
  datas?: GoodsItemData[]
}
// 评价模型
export interface EvaluationItemData {
    id: number,
    user: UserInfo,
    gid: number,
    content: string,
    star: number,
    created_at: string,
    state: number
}
export interface EvaluationResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: EvaluationItemData[]
}
// 数据统计模型
export interface StatisticalItemData {
    allCount: number,
    datas:{date: string, count: number}[]
}
export interface StatisticalResponseData {
    msg: 'error' | 'ok',
    error?: string,
    datas?: StatisticalItemData[]
}
// 发起请求的方法
function startRequest(path: RequestPath, method: Method, params: any) {
  const config: any = {
    method: method,
    url: path
  };
  // GET方法将参数放在params中
  if (method === 'GET' || method === 'get') {
    config.params = params;
  } 
  // POST方法将参数放在data中
  else {
    config.data = params;
  }
  // 返回一个Promise
  return worker.request(config);
}
// 导出
export {
  worker,
  RequestPath,
  startRequest
};